package org.imlinker.gui;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

@SuppressWarnings("serial")
public class TextAreaSupportWinodw extends LocationSavedWindow implements
		MouseListener {

	private JPopupMenu menu = null;

	private JMenuItem copy;

	private JMenuItem paste;

	private JMenuItem cut;

	private JTextArea area;

	private void initPopupMenu() {
		copy = new JMenuItem("Copy");
		paste = new JMenuItem("Paste");
		cut = new JMenuItem("Cut");
		copy.setAccelerator(KeyStroke.getKeyStroke('C', InputEvent.CTRL_MASK));
		paste.setAccelerator(KeyStroke.getKeyStroke('V', InputEvent.CTRL_MASK));
		cut.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_MASK));

		copy.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				area.copy();
			}
		});

		paste.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				area.paste();
			}
		});

		cut.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				area.cut();
			}
		});

		menu = new JPopupMenu();
		menu.add(copy);
		menu.add(paste);
		menu.add(cut);

		area.add(menu);
	}

	private boolean isCanCopy() {
		boolean b = false;
		int start = area.getSelectionStart();
		int end = area.getSelectionEnd();
		if (start != end) {
			b = true;
		}

		return b;
	}

	private boolean isCanCut() {
		boolean b = false;
		int start = area.getSelectionStart();
		int end = area.getSelectionEnd();
		if (start != end) {
			b = true;
		}

		if (!area.isEditable()) {
			return false;
		}

		return b;
	}

	private boolean isClipboardString() {
		boolean b = false;
		Clipboard clipboard = area.getToolkit().getSystemClipboard();
		Transferable content = clipboard.getContents(area);
		try {
			if (content.getTransferData(DataFlavor.stringFlavor) instanceof String) {
				b = true;
			}
		} catch (Exception e) {
		}

		if (!area.isEditable()) {
			return false;
		}

		return b;
	}

	@Override
	public void mousePressed(MouseEvent e) {
		JTextArea tempArea;
		if (e.getSource() instanceof JTextArea) {
			tempArea = (JTextArea) e.getSource();
			this.area = tempArea;
		} else {
			return;
		}

		if (e.getButton() == MouseEvent.BUTTON3) {
			if (menu == null) {
				initPopupMenu();
			}

			copy.setEnabled(isCanCopy());
			paste.setEnabled(isClipboardString());
			cut.setEnabled(isCanCut());
			menu.show(tempArea, e.getX(), e.getY());
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {

	}

}
